// 导入
require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.less')
require('../font/iconfont.css')
require('../css/motion-details.less')

// 导入axios
const axios = require('axios')

// 导入weui
require('weui')
const weui = require('weui.js')


document.addEventListener('DOMContentLoaded', function () {
    // 获取元素
    const goBack = document.querySelector('#goBack')
    const video = document.querySelector('video')
    const calorie = document.querySelector('#calorie')
    const desc = document.querySelector('#desc')
    const frequency = document.querySelector('#frequency')
    const imgurl = document.querySelector('#imgurl')
    const instrument = document.querySelector('#instrument')
    const name = document.querySelector('#name')
    const peoplenum = document.querySelector('#peoplenum')
    const time = document.querySelector('#time')
    const videoPlay = document.querySelector('#videoPlay')
    const startBtn = document.querySelector('#startBtn')


    // 点击按钮返回上一级页面
    goBack.addEventListener('click', function () {
        history.go(-1);
    })
    // 发送请求从后台拉取数据
    const cid = sessionStorage.cid
    // console.log(cid);
    axios.get('http://139.9.177.51:8099/sports/courseDetail?id=' + cid).then(function (res) {
        // console.log(res.data.data);
        const data = res.data.data

        const fragments = data.fragments
        sessionStorage.fragments = JSON.stringify(fragments)
        // 视频
        video.src = 'http://139.9.177.51:8099' + data.fragments[1].videoUrl
        // 卡路里
        calorie.textContent = data.calorie
        // 时间
        time.textContent = data.time
        // 观看人数
        peoplenum.textContent = data.peoplenum
        // 介绍
        desc.textContent = data.desc
        // 次数
        frequency.textContent = `每周${data.frequency}次`
        // 教练头像
        imgurl.src = `http://139.9.177.51:8099${data.imgurl}`
        // 器械
        instrument.textContent = data.instrument
        // 课程名
        name.textContent = data.name
    })

    // 点击按钮开始播放视频
    let flag = false;
    videoPlay.addEventListener('click', function () {
        flag = !flag
        if (flag === true) {
            video.play()
            videoPlay.style.display = 'none'
        } else {
            video.pause()
            videoPlay.style.display = 'flex'
        }
    })
    video.addEventListener('click', function () {
        flag = !flag
        if (flag === true) {
            video.play()
            videoPlay.style.display = 'none'
        } else {
            video.pause()
            videoPlay.style.display = 'flex'
        }
    })
    video.addEventListener('ended', function () {
        video.play()
    })
    // 点击开始训练弹出提示确定后跳转
    startBtn.addEventListener('click', function () {
        weui.confirm('是否确认开始训练', function () {
            location.href = './train.html'
        })
    })
})